package settings

import (
	"github.com/go-ini/ini"
	"time"
	"blog/pkg/utils/logging"
)

var (
	Cfg     *ini.File
	RunMode string

	HttpPort int

	ReadTimeOut  time.Duration
	WriteTimeOut time.Duration

	PageSize  int
	JwtSecret string
)

func init() {
	var err error
	Cfg, err = ini.Load("src/blog/conf/api.ini")

	if err != nil {
		logging.Error.Fatalf("Fail to parse 'conf/app.ini': %v", err)
	}

	LoadBaseMode()
	LoadServer()
	LoadApp()
}

func LoadBaseMode() {
	RunMode = Cfg.Section("").Key("RUN_MODE").MustString("debug")
}

func LoadServer() {
	sec, err := Cfg.GetSection("server")

	if err != nil {
		logging.Error.Fatalf("Fail to get section 'server' : %v", err)
	}

	RunMode = Cfg.Section("").Key("RUN_MODE").MustString("debug")

	HttpPort = sec.Key("HTTP_PORT").MustInt(8000)

	ReadTimeOut = time.Duration(sec.Key("READ_TIMEOUT").MustInt(60)) * time.Second
	WriteTimeOut = time.Duration(sec.Key("WRITE_TIMEOUT").MustInt(60)) * time.Second
}

func LoadApp() {
	sec, err := Cfg.GetSection("app")

	if err != nil {
		logging.Error.Fatalf("Fail to get section 'app' : %v", err)
	}

	JwtSecret = sec.Key("JWT_SECRET").MustString("!@)*#)!@U#@*!@!)")
	PageSize = sec.Key("PAGE_SIZE").MustInt(10)
}
